package me.pplay.playtalk.database;

import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.LogManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


public class UserInfoDb {
	private final String USERINFO_TBNAME 	= "userinfo";
	private DbHelper dbHelper;
	public UserInfoDb(){}
	
	public UserInfoDb(Context context){
		dbHelper	= new DbHelper(context);
	}
	
	/**
	 * 判断
	 * @return
	 */
	public boolean isExists(int skyid){
		Cursor cursor = null;
		try {
			dbHelper.openDatabase();
			cursor = dbHelper.db.query(USERINFO_TBNAME, null, "skyid=" + skyid + "", null, null,null, null);
			if (cursor.getCount() > 0) {
				return true;
			}
		} catch (Exception e) {
		} finally {
			if(cursor != null)
				cursor.close();
			dbHelper.closeDatabase();
		}
		
		return false;
	}
	
	/**
	 * 添加
	 * @return
	 */
	public boolean add(TPBuddyinfo buddy){
		dbHelper.openDatabase();
		boolean bool = false;
		long result = 0;
		try{
			ContentValues values = new ContentValues();
			values.put("skyid", buddy.getSkyid());
			values.put("name", buddy.getName());
			values.put("alias", buddy.getAlias());
			values.put("server_alias", buddy.getServer_alias());
			values.put("birthday", buddy.getBirthday());
			values.put("country", buddy.getCountry());
			values.put("signature", buddy.getSignature());
			values.put("pnum", buddy.getPnum());
			values.put("sex", buddy.getSex());
			values.put("age", buddy.getAge());
			values.put("searchvisible", buddy.getSearchvisible());
			values.put("status", buddy.getStatus());
			values.put("lanstr", buddy.getLanstr());
			values.put("lan", buddy.getLan());
			values.put("avatarid", buddy.getAvatarid());
			values.put("avatar_sid", buddy.getAvatar_sid());
			values.put("avatar_fpath", buddy.getAvatar_fpath());
			result = dbHelper.db.insert(USERINFO_TBNAME, null, values);
		}catch (Exception e) {
			e.printStackTrace();
			LogManager.LogShow("ChattingRecordDb", e.getMessage());
		}finally{
			dbHelper.closeDatabase();
			if(result >0)
				bool = true;
		}
		return bool;
	}
	
	/**
	 * 更新数据
	 */
	public void update(TPBuddyinfo buddy) {
		dbHelper.openDatabase();
		ContentValues values;
		try {
			if (buddy != null) {
				values = new ContentValues();
				values.put("skyid", buddy.getSkyid());
				values.put("name", buddy.getName());
				values.put("alias", buddy.getAlias());
				values.put("server_alias", buddy.getServer_alias());
				values.put("birthday", buddy.getBirthday());
				values.put("country", buddy.getCountry());
				values.put("signature", buddy.getSignature());
				values.put("pnum", buddy.getPnum());
				values.put("sex", buddy.getSex());
				values.put("age", buddy.getAge());
				values.put("searchvisible", buddy.getSearchvisible());
				values.put("status", buddy.getStatus());
				values.put("lanstr", buddy.getLanstr());
				values.put("lan", buddy.getLan());
				values.put("avatarid", buddy.getAvatarid());
				values.put("avatar_sid", buddy.getAvatar_sid());
				values.put("avatar_fpath", buddy.getAvatar_fpath());
				dbHelper.db.update(USERINFO_TBNAME, values, "skyid=" + buddy.getSkyid(), null);
			}
		} catch (Exception e) {
		} finally {
			dbHelper.closeDatabase();
		}
	}
	
	
	/**
	 * 查询
	 */
	public TPBuddyinfo query(int skyid){
		dbHelper.openDatabase();
		TPBuddyinfo	buddy				= null;
		Cursor cursor 					= null;
		try {
			String sql					= "select * from " + USERINFO_TBNAME + " where skyid="+skyid;
			cursor 						= dbHelper.db.rawQuery(sql, null);
			if(cursor == null)
				return null;
			if (cursor.moveToNext()) {
				buddy	= new TPBuddyinfo();
				buddy.setSkyid(skyid);
				buddy.setName(cursor.getString(cursor.getColumnIndex("name")));
				buddy.setAlias(cursor.getString(cursor.getColumnIndex("alias")));
				buddy.setServer_alias(cursor.getString(cursor.getColumnIndex("server_alias")));
				buddy.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
				buddy.setCountry(cursor.getString(cursor.getColumnIndex("country")));
				buddy.setSignature(cursor.getString(cursor.getColumnIndex("signature")));
				buddy.setPnum(cursor.getString(cursor.getColumnIndex("pnum")));
				buddy.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
				buddy.setAge(cursor.getInt(cursor.getColumnIndex("age")));
				buddy.setSearchvisible(cursor.getInt(cursor.getColumnIndex("searchvisible")));
				buddy.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
				buddy.setLanstr(cursor.getString(cursor.getColumnIndex("lanstr")));
				buddy.setLan(cursor.getString(cursor.getColumnIndex("lan")));
				buddy.setAvatarid(cursor.getInt(cursor.getColumnIndex("avatarid")));
				buddy.setAvatar_sid(cursor.getInt(cursor.getColumnIndex("avatar_sid")));
				buddy.setAvatar_fpath(cursor.getString(cursor.getColumnIndex("avatar_fpath")));
            }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			dbHelper.closeDatabase();
		}
		return buddy;
	}
	
	/**
	 * 删除
	 */
	public void delete(int skyid) {
		dbHelper.openDatabase();
		dbHelper.db.delete(USERINFO_TBNAME, "skyid=" + skyid + "", null);
		dbHelper.closeDatabase();
	}
	
}
